package com.sfh.cp.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import com.sfh.cp.R;
import com.sfh.cp.base.AbstractBaseActivity;
import com.sfh.cp.mass.MassAddContentActivity;
import com.sfh.cp.mass.MassFragment;
import com.sfh.cp.msg.MsgFragment;
import com.sfh.cp.user.UserFragment;
import com.sfh.cp.user.login.UserLoginActivity;
import com.sfh.cp.widget.WidgetTableMenuLayout;
import com.sfh.lib.AppCacheManager;
import com.sfh.lib.mvp.IPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import crm.sfh.server.Const;
import crm.sfh.server.SUser;

/**
 * Created by Administrator on 2018/3/6.
 */

public class HomeActivity extends AbstractBaseActivity {


    public static void startUI(Context context) {
        Intent intent = new Intent(context, HomeActivity.class);
        context.startActivity(intent);
    }

    final String TABLE_HOME = "home";
    final String TABLE_MASS = "mass";
    final String TABLE_MSG = "msg";
    final String TABLE_USER = "user";

    SparseArray<Fragment> content = new SparseArray<>(4);
    FragmentManager mFragmentManager;
    Fragment indexFragment;

    @BindView(R.id.table)
    WidgetTableMenuLayout table;
    @BindView(R.id.btAdd)
    ImageView btAdd;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_activity);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        mFragmentManager = getSupportFragmentManager();
        this.indexFragment = doTabChanged(indexFragment, TABLE_HOME);
        table.setOnSelectItem(new WidgetTableMenuLayout.IOnSelectItem() {
            @Override
            public void onSelectItem(int position) {
                String tag = position == 0 ? TABLE_HOME : position == 1 ? TABLE_MASS : position == 2 ? TABLE_MSG : TABLE_USER;

                indexFragment = doTabChanged(indexFragment, tag);
            }
        });
    }


    private Fragment doTabChanged(Fragment oldFragment, String tag) {

        FragmentTransaction ft = mFragmentManager.beginTransaction();
        if (oldFragment != null) {
            ft.detach(oldFragment);
        }
        Fragment newFragment = mFragmentManager.findFragmentByTag(tag);
        if (newFragment == null) {
            newFragment = content.get(tag.hashCode());
            if (newFragment == null) {
                newFragment = createFragment(tag);
                content.put(tag.hashCode(), newFragment);
            }
            ft.add(R.id.fyContent, newFragment, tag);
        } else {
            ft.attach(newFragment);
        }
        ft.commit();
        return newFragment;
    }

    Fragment createFragment(String tag) {
        if (TextUtils.equals(TABLE_HOME, tag)) {
            return HomeFragment.newFragment();
        }
        if (TextUtils.equals(TABLE_MASS, tag)) {
            return new MassFragment();
        }
        if (TextUtils.equals(TABLE_MSG, tag)) {
            return new MsgFragment();
        }
        return new UserFragment();
    }

    @Override
    public IPresenter getPresenter() {
        return null;
    }

    @OnClick(R.id.btAdd)
    public void onViewClicked() {
        SUser user = AppCacheManager.newInstance().getCache(Const.USER, new SUser());
        if (TextUtils.isEmpty(user.getObjectId())) {
            UserLoginActivity.startUI(this);
            return;
        }
        MassAddContentActivity.startUI(this);
    }
    float y1 = 0;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                y1 = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                float y2 = ev.getY();
                if (y1 - y2 > 50) {
                    //向上滑
                    if (table.isShown()){
                        table.setVisibility(View.GONE);
                        btAdd.setVisibility(View.GONE);
                    }

                } else if (y2 - y1 > 50) {
                    //向下滑
                    if (!table.isShown()){
                        table.setVisibility(View.VISIBLE);
                        btAdd.setVisibility(View.VISIBLE);
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                y1 = 0;
                break;
            default:
                break;
        }
        return super.dispatchTouchEvent(ev);
    }
}
